/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.beans.java;

import ca.trader.beans.enterprise.UserBeanLocal;
import ca.trader.except.UserDoesNotExistException;
import ca.trader.except.UserNotAuthenticatedException;
import ca.trader.logger.TraderLogger;
import ca.trader.user.User;
import javax.servlet.http.HttpSession;

/**
 * A regular JavaBean to look up a user. The options to look up users are: - by
 * ID the authenticated user
 *
 * To find a user by ID, call the setUserId method To find the authenticated
 * user, call the setSession method
 *
 * @author Ahsan
 */
public class UserLookupBean
{

	 private static final String _className = UserLookupBean.class.getName();
	 // The user entity object that will contain the looked up user
	 private User user;
	 private boolean foundResults = false;

	 /**
	  * Sets the session object and looks up the authenticated user
	  *
	  * @param sess the session object
	  */
	 public void setSession(HttpSession sess)
	 {
		  final String _methodName = "setSession";

		  TraderLogger.logMethodEntry(_className, _methodName);
		  TraderLogger.logInfo(_className, _methodName, "Trying to find authenticated user");

		  user = null;
		  foundResults = false;
		  
		  try
		  {

			   UserBeanLocal userBean = BeanUtils.getUserBean();

			   // Get the authenticated user
			   user = userBean.getAuthenticatedUser(sess);
			   TraderLogger.logInfo(_className, _methodName, "Found user, email = " + user.getEmail());

			   foundResults = true;
		  }
		  // If no authenticated user is found
		  catch (UserNotAuthenticatedException ex)
		  {
			   TraderLogger.logInfo(_className, _methodName, "No authenticated user exists.");
		  }
		  catch (Exception ex)
		  {
			   TraderLogger.logSevere(_className, _methodName, "Unknown exception ", ex);
		  }

		  TraderLogger.logMethodExit(_className, _methodName);
	 }

	 /**
	  * Finds a user by userID
	  *
	  * @param sID the user id
	  */
	 public void setUserId(String sID)
	 {
		  final String _methodName = "setUserId";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  TraderLogger.logInfo(_className, _methodName, "Finding user with ID: " + sID);


		  user = null;
		  foundResults = false;

		  // if the ID is null
		  if (sID == null)
		  {
			   TraderLogger.logInfo(_className, _methodName, "ID is null. Doing nothing");
		  }
		  else
		  {
			   try
			   {

				    Integer id = Integer.parseInt(sID);

				    UserBeanLocal userBean = BeanUtils.getUserBean();

				    // Get the user with the ID
				    user = userBean.getUserWithID(id);

				    TraderLogger.logInfo(_className, _methodName, "Found user, email = " + user.getEmail());

				    foundResults = true;

			   }
			   // If the id is not a number
			   catch (NumberFormatException nfe)
			   {
				    TraderLogger.logWarning(_className, _methodName, "ID is not an integer.  Doing nothing.");
			   }
			   // If no user ound with the ID
			   catch (UserDoesNotExistException nfe)
			   {
				    TraderLogger.logWarning(_className, _methodName, "No user found with the matching ID.  Doing nothing.");
			   }
			   // Other exception
			   catch (Exception e)
			   {
				    TraderLogger.logSevere(_className, _methodName, "Unknown exception ", e);
			   }
		  }

		  TraderLogger.logMethodExit(_className, _methodName);

	 }

	 /**
	  *
	  * @return if the user was found
	  */
	 public boolean isUserExistent()
	 {
		  return foundResults;
	 }

	 public String getUserId()
	 {
		  return user.getId() + "";
	 }

	 /**
	  *
	  * @return the first name of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getFirstName()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getFirstName();
	 }

	 /**
	  *
	  * @return the last name of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getLastName()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getLastName();
	 }

	 /**
	  *
	  * @return the email of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getEmail()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getEmail();
	 }

	 /**
	  *
	  * @return the city of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getCity()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getCity();
	 }

	 /**
	  *
	  * @return the region of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getRegion()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getRegion().getRegionName();
	 }

	 /**
	  *
	  * @return the country of the user that was found
	  * @exception IllegalStateException if no user found
	  */
	 public String getCountry()
	 {
		  if (!foundResults)
		  {
			   throw new IllegalStateException("User has not been found.");
		  }

		  return user.getCountry().getCountryName();
	 }

}
